package com.application.chiettinhdienluc.adapter;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.application.chiettinhdienluc.R;
import com.application.chiettinhdienluc.adapter.HoSoChietTinhItemPopMenuAdapter.HoSoItemPopupMenuItem;
import com.application.chiettinhdienluc.model.ChietTinhModel;
import com.application.chiettinhdienluc.ui.DanhMucHsoActivity;
import com.application.chiettinhdienluc.utils.Log;

public class DanhMucHoSoAdapter extends BaseAdapter{

	private final String TAG = DanhMucHoSoAdapter.class.getSimpleName();
	public DanhMucHsoActivity context;
	public LayoutInflater inflater;
	private ArrayList<ChietTinhModel> itemList;	
	private boolean isScrolling = false;	

	public DanhMucHoSoAdapter(DanhMucHsoActivity context, ArrayList<ChietTinhModel> itemList) {
		super();
		this.context = context;
		this.itemList = new ArrayList<ChietTinhModel>(itemList);
		this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);		
	}

	public void setScrollingStatus(boolean isScrolling)
	{
		this.isScrolling = isScrolling;
	}

	public ArrayList<ChietTinhModel> getItemList() {
		return itemList;
	}

	public void setItemList(ArrayList<ChietTinhModel> itemList) {
		this.itemList = new ArrayList<ChietTinhModel>(itemList);	
	}

	@Override
	public int getCount() {
		return itemList.size();
	}

	@Override
	public ChietTinhModel getItem(int position) {
		return itemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	public static class ViewHolder
	{
		// item msg row
		//public ImageView pop_up_anchor;
		public TextView tv_ma_ban_tinh;
		public TextView tv_ten_khach_hang;
		public TextView tv_phone;
		public TextView tv_dia_chi;
		public TextView tv_ngay_nop_don_value;
		public TextView tv_ngay_khao_sat_value;
		public TextView tv_nhan_vien_khao_sat_value;
		public TextView tv_trang_thai_ho_so;
		public CheckBox checkboxToDelete;
		public ImageButton btn_submit;
		
		public int position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {		
		ViewHolder holder;
		if(convertView == null)
		{
			holder = new ViewHolder();

			convertView = inflater.inflate(com.application.chiettinhdienluc.R.layout.danh_muc_hso_chiet_tinh_item, parent, false);

			//holder.pop_up_anchor = (ImageView) convertView.findViewById(R.id.pop_up_anchor);
			holder.tv_ma_ban_tinh = (TextView) convertView.findViewById(R.id.tv_ma_ban_tinh);
			holder.tv_ten_khach_hang = (TextView) convertView.findViewById(R.id.tv_ten_khach_hang);
			holder.tv_phone = (TextView) convertView.findViewById(R.id.tv_phone);
			holder.tv_dia_chi = (TextView) convertView.findViewById(R.id.tv_dia_chi);
			holder.tv_ngay_nop_don_value = (TextView) convertView.findViewById(R.id.tv_ngay_nop_don_value);
			holder.tv_ngay_khao_sat_value = (TextView) convertView.findViewById(R.id.tv_ngay_khao_sat_value);
			holder.tv_nhan_vien_khao_sat_value = (TextView) convertView.findViewById(R.id.tv_nhan_vien_khao_sat_value);
			holder.tv_trang_thai_ho_so = (TextView) convertView.findViewById(R.id.tv_trang_thai_ho_so);
			holder.checkboxToDelete = (CheckBox)convertView.findViewById(R.id.checkboxToDelete);
			holder.btn_submit = (ImageButton)convertView.findViewById(R.id.btn_submit);
			//holder.pop_up_anchor.setTag(holder);

			convertView.setTag(holder);
		}
		else
			holder = (ViewHolder)convertView.getTag();		

		try {		
			holder.position = position;
			
			ChietTinhModel item = getItem(position);

			Resources res = context.getResources();

			holder.tv_ma_ban_tinh.setText(item.get_MaBanChietTinh());
			holder.tv_ten_khach_hang.setText(item.get_TenKhachHang());
			holder.tv_phone.setText(item.get_DienThoaiDichVu());
			holder.tv_dia_chi.setText(item.get_DiaChi());
			holder.tv_ngay_nop_don_value.setText(item.get_NgayND());
			holder.tv_ngay_khao_sat_value.setText(item.get_NgayKhaoSat());
			holder.tv_nhan_vien_khao_sat_value.setText(item.get_NhanVienKhaoSat());
			holder.tv_trang_thai_ho_so.setText(context.getTrangThaiHoSo(position));

			/*
			holder.pop_up_anchor.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
				
//					context.registerForContextMenu(v);
//					context.openContextMenu(v);
//					context.unregisterForContextMenu(v);

					final ArrayList<HoSoItemPopupMenuItem> items = new ArrayList<HoSoItemPopupMenuItem>();	
					HoSoItemPopupMenuItem viewHoSoItem = new HoSoItemPopupMenuItem(0,	context.getString(R.string.str_popup_item_view));
					HoSoItemPopupMenuItem submitHoSoItem = new HoSoItemPopupMenuItem(1,	context.getString(R.string.str_popup_item_submit));
					
					items.clear();
					items.add(viewHoSoItem);
					items.add(submitHoSoItem);

					final HoSoChietTinhItemPopMenuAdapter adapter = new HoSoChietTinhItemPopMenuAdapter(context);
					adapter.setItemList(items);

					AlertDialog.Builder builder = new AlertDialog.Builder(context);
					builder.setAdapter(adapter, new DialogInterface.OnClickListener() 
					{
						public void onClick(DialogInterface di, int which) 
						{
							int itemId = adapter.getMenuItemId(which);
							if(itemId == -1)
								return;
							
							if(itemId == 0)
							{
								context.goToViewChietTinhDetail(position);
							}
							else if(itemId == 1)
							{
							} 
						}
					});
					AlertDialog alert = builder.create();
					alert.setCanceledOnTouchOutside(true);
					alert.show();
				}
			});
			*/
			
//			holder.tv_ten_khach_hang.setOnClickListener(new View.OnClickListener() {
//				@Override
//				public void onClick(View v) {
//					// TODO Auto-generated method stub
//					context.goToViewChietTinhDetail(position);
//				}
//			});
			
			holder.btn_submit.setVisibility(item.get_TrangThaiHoSo().equals("2") ? View.VISIBLE : View.GONE);
			
			String str_status = item.get_TrangThaiHoSo().toString();
			
			if (str_status.equals("1")){
				holder.tv_trang_thai_ho_so.setTextColor(Color.parseColor("#008000"));
			}
			else if (str_status.equals("2")){
				holder.tv_trang_thai_ho_so.setTextColor(Color.parseColor("#FF7E00"));
			}
			else{
				holder.tv_trang_thai_ho_so.setTextColor(Color.RED);
			}
			
			holder.btn_submit.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					context.submitHoSo(position);
				}
			});
			
			holder.checkboxToDelete.setChecked(item.isCheckToDelete());
			
			final boolean bCheck = !holder.checkboxToDelete.isChecked();
			holder.checkboxToDelete.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					context.setCheckOrUnCheck(position, bCheck);
				}
			});
			
			convertView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					context.goToViewChietTinhDetail(position);
				}
			});

		} catch (Exception e) {
			e.printStackTrace();
		}		

		return convertView;
	}	
	
}
